package screen;

import javax.microedition.lcdui.Graphics;

import main.GameCanvas;
import model.Command;
import utilities.TField;
import font.BitmapFont;

public class InputScr extends Screen {
	private static InputScr instance;
	public static InputScr gI(){
		return (instance==null?instance=new InputScr():instance);		
	}
	private TField[] list;
	private String[] name;
	public int selected=0;
	private Screen lastScr;
	public String title,subTitle;
	
	public void setInfo(TField[] lis,String[] name,Command cmdCenter, Command cmdLeft,String title,String subTitle){
		m_cmdCenter = cmdCenter;
		m_cmdLeft = cmdLeft;
		
		this.title = title;
		this.subTitle = subTitle;
		this.name = name;
		m_height = 50 + lis.length * 45;		
		m_width = 160;		
		if(GameCanvas.w <= 128)
			m_width=100;					
		list=lis;
		selected=0;
		for(int i=0;i<list.length;i++){
			list[i].width = m_width - 50;	
			list[i].height = ITEM_HEIGHT + 2;
			list[i].y = GameCanvas.hh- m_height/2+63+i*45;	
			list[i].x=GameCanvas.hw-(list[i].width)/2;
		}
		list[0].isFocus=true;
		m_cmdRight=list[0].cmdClear;		
	}
	public void update() {
		for(int i=0;i<list.length;i++){						
			list[i].update();			
		}
		lastScr.update();
	}
	public void keyPress(int keyCode) {
		for(int i=0;i<list.length;i++){			
			if(list[i].isFocus){
				list[i].keyPressed(keyCode);
				break;
			}
		}
		super.keyPress(keyCode);
	}
	public void updateKey(){
		if(GameCanvas.isPointerClick){
			if(GameCanvas.isPointer(list[0].x, list[0].y, list[0].width, list[0].height)){
				list[0].isFocus = true;
				list[1].isFocus = false;
			}
			if(GameCanvas.isPointer(list[1].x, list[1].y, list[1].width, list[1].height)){
				list[1].isFocus = true;
				list[0].isFocus = false;
			}
		}
		
		if (GameCanvas.keyPressed[8]) {
			GameCanvas.keyPressed[8] = false;			
			selected++;
			if(selected>=list.length)
				selected=0;
			setFocus();
		} else if (GameCanvas.keyPressed[2]) {
			GameCanvas.keyPressed[2] = false;			
			selected--;
			if(selected<0)
				selected=list.length-1;
			setFocus();
		} 
		super.updateKey();
	}
	private void setFocus() {
		for(int i=0;i<list.length;i++){			
			if(list[i].isFocus){
				list[i].isFocus=false;
				break;
			}
		}	
		list[selected].isFocus=true;
		m_cmdRight=list[selected].cmdClear;
	}
	public void paint(Graphics g){
		lastScr.paint(g);			
		PaintPopup.paintInputScrBG(g,GameCanvas.hw - m_width/2-1, GameCanvas.h / 2 - m_height/2-1, m_width + 2, m_height + 2,title, subTitle);
		for(int i=0;i<list.length;i++){	
			g.setClip(0, 0, GameCanvas.w, GameCanvas.h);
			BitmapFont.drawNormalFont(g, name[i], list[i].x, list[i].y-15, 0x000000, Graphics.LEFT);

			list[i].y = GameCanvas.h / 2 - m_height/2+63+i*45;	
			list[i].paint(g);
		}
		super.paint(g);
	}
	public void switchToMe(Screen mainMenuScr) {		
		lastScr=mainMenuScr;
		super.switchToMe();
	}
	public void close() {
		// TODO Auto-generated method stub
		lastScr.switchToMe();
		list=null;
		name=null;
		m_cmdCenter=null;
		lastScr=null;
	}
}

















